package com.zlzlib.libdialog;

import android.content.Context;


import com.zlzlib.libdialog.databinding.DialogDateTimeSelectBinding;

import java.util.Calendar;

/**
 * 日期时间 同时选择器
 *
 * @DateTime: 2022/9/20 17:38
 * @Author zlz
 * @Version 1.0
 */
public class DateTimeSelectDialog extends BaseDialog<DialogDateTimeSelectBinding> {

    private int year, monthOfYear, dayOfMonth, hourOfDay, minute;
    private DateTimeSelectListener dateTimeSelectListener;
    private final Calendar calendar;

    public DateTimeSelectDialog(Context context) {
        this(context, Calendar.getInstance());
    }

    public DateTimeSelectDialog(Context context, Calendar calendar) {
        super(context);
        this.calendar = calendar;
        initDataWindow();
    }

    @Override
    protected int getContentViewId() {
        return R.layout.dialog_date_time_select;
    }

    private void initDataWindow() {
        setDialogWH(1, 0);
        mBinding.tvLeft.setOnClickListener(v -> dismiss());
        mBinding.tvRight.setOnClickListener(v -> {
            dismiss();
            if (dateTimeSelectListener != null)
                dateTimeSelectListener.selectDateTime(year, monthOfYear, dayOfMonth, hourOfDay, minute);
        });
        mBinding.datePicker.init(year = calendar.get(Calendar.YEAR), monthOfYear = calendar.get(Calendar.MONTH),
                dayOfMonth = calendar.get(Calendar.DAY_OF_MONTH), (view, year, monthOfYear, dayOfMonth) -> {
                    this.year = year;
                    this.monthOfYear = monthOfYear;
                    this.dayOfMonth = dayOfMonth;
                });
        mBinding.timePicker.setIs24HourView(true);
        mBinding.timePicker.setCurrentHour(hourOfDay = calendar.get(Calendar.HOUR_OF_DAY));
        mBinding.timePicker.setCurrentMinute(minute = calendar.get(Calendar.MINUTE));
        mBinding.timePicker.setOnTimeChangedListener((view, hourOfDay, minute) -> {
            this.hourOfDay = hourOfDay;
            this.minute = minute;
        });
    }

    public void setDateTimeSelectListener(DateTimeSelectListener dateTimeSelectListener) {
        this.dateTimeSelectListener = dateTimeSelectListener;
    }

    public interface DateTimeSelectListener {
        void selectDateTime(int year, int monthOfYear, int dayOfMonth, int hourOfDay, int minute);
    }
}
